//
//  NSObject+ObjectToDictionary.m

#import "NSObject+ObjectToDictionary.h"
#import <objc/runtime.h>

@implementation NSObject (ObjectToDictionary)

- (NSDictionary *)dictionaryWithProperties
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    unsigned count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    for (int i = 0; i < count; i++) {
        NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
        if ([self valueForKey:key]) {
            [dict setObject:[self valueForKey:key] forKey:key];
        }
    }

    free(properties);

    return [NSDictionary dictionaryWithDictionary:dict];
}

@end
